﻿using Abp.Domain.Repositories;
using Abp.Domain.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SingleUserMall.Items
{
    public class ItemManager : DomainService
    {
        private readonly IRepository<ItemComment, long> _itemCommentRepository;
        private readonly IRepository<Item, long> _itemRepository;
        public ItemManager(
            IRepository<ItemComment, long> itemCommentRepository,
            IRepository<Item, long> itemRepository
            )
        {
            _itemCommentRepository = itemCommentRepository;
            _itemRepository = itemRepository;
        }

        public double GetItemStar(long itemId)
        {
            return _itemCommentRepository.GetAll()
                .Where(x => x.ItemId == itemId)
                .Average(x => (double?)x.Star) ?? 0;
        }

        public IQueryable<Item> GetItemsByStoreId(int storeId)
        {
            return _itemRepository.GetAll()
                .Where(x => x.StoreId == storeId && x.IsSale).OrderByDescending(x => x.CreationTime).Take(3);
        }


        /// <summary>
        /// 验证批发商品是否合法
        /// </summary>
        /// <param itemId="itemId" qty="qty"></param>
        /// <returns></returns>
        public bool ValidateWholesale(long itemId, int qty, out int validCount)
        {
            var item = _itemRepository.FirstOrDefault(itemId);
            validCount = item.MinWholesale;
            if (!item.IsWholesale)
            {
                return true;
            }
            else
            {
                return qty >= item.MinWholesale;
            }
        }

        /// <summary>
        /// 下架所有商品
        /// </summary>
        /// <param storeId="storeId">店铺ID</param>
        /// <returns></returns>
        public void SetItemUnSale(int storeId)
        {
            var items = _itemRepository.GetAll().Where(x => x.StoreId == storeId && x.IsSale).ToList();
            items.ForEach(item => {
                item.IsSale = false;
            });
        }

        public void ComputeItemPrice(Item item)
        {
            var itemSpec = item.ItemSpecs.OrderBy(x => x.Price).FirstOrDefault();
            if(itemSpec == null)
            {
                item.Price =  0;
                item.VipPrice = 0;
                item.SuperVipPrice = 0;
                item.GroupPrice = 0;
                item.OriginalPrice = 0;
                item.LimitPrice = 0;
                item.PerPrice = 0;
                item.BargainPrice = 0;
            }
            else
            {
                item.Price = itemSpec.Price;
                item.VipPrice = itemSpec.VipPrice;
                item.SuperVipPrice = itemSpec.SuperVipPrice;
                item.GroupPrice = itemSpec.GroupPrice;
                item.OriginalPrice = itemSpec.OriginalPrice;
                item.LimitPrice = itemSpec.LimitPrice;
                item.PerPrice = itemSpec.PerPrice;
                item.BargainPrice = itemSpec.BargainPrice;
            }
        }
    }
}
